{extend name="../../admin/view/table"}

{block name="button"}
<!--{if auth("add")}-->
<button class='layui-btn layui-btn-sm layui-btn-primary' data-table-id="UpgradeTable" data-modal="{:url('add')}">添加等级
</button>
<!--{/if}-->

<!--{if auth("sync")}-->
<button class='layui-btn layui-btn-sm layui-btn-primary' data-queue="{:url('sync')}">同步用户</button>
<!--{/if}-->
{/block}

{block name="content"}
<div class="think-box-shadow">
    {include file='base/upgrade/index_search'}
    <table id="UpgradeTable" data-url="{:sysuri()}" data-target-search="form.form-search"></table>
</div>
{/block}

{block name='script'}
<script>
    $(function () {
        // 初始化表格组件
        $('#UpgradeTable').layTable({
            even: true, height: 'full',
            sort: {field: 'number', type: 'asc'},
            cols: [[
                {field: 'number', title: '序号', align: "center", width: 80, sort: true},
                {field: 'name', title: '等级名称', align: 'center', minWidth: 100},
                {
                    field: 'upgrade_team', title: '团队计数', align: 'center', width: 80, templet: function (d) {
                        if (!d.upgrade_team || d.upgrade_team < 1) return '-';
                        return '<b class="layui-icon layui-icon-ok-circle color-green"></b>';
                    }
                },
                {
                    field: 'upgrade_type', title: '升级规则', align: 'center', width: 80, templet: function (d) {
                        if (d.upgrade_type === 1) return '<span class="color-green">全部完成</span>';
                        else return '<span class="color-blue">任何条件</span>';
                    }
                },
                {
                    field: 'goods_vip_status', title: '入会礼包', align: 'center', width: 80, templet: function (d) {
                        if (!d.goods_vip_status || d.goods_vip_status < 1) return '-';
                        return '<b class="layui-icon layui-icon-ok-circle color-green"></b>';
                    }
                },
                {
                    field: 'teams_users_status', title: '团队总数', align: 'center', width: 80, templet: function (d) {
                        if (!d.teams_users_status || d.teams_users_status < 1) return '-';
                        return laytpl('<b>{{d.teams_users_number}}</b>').render(d);
                    }
                },
                {
                    field: 'teams_direct_status', title: '直属团队', align: 'center', width: 80, templet: function (d) {
                        if (!d.teams_direct_status || d.teams_direct_status < 1) return '-';
                        return laytpl('<b>{{d.teams_direct_number}}</b>').render(d);
                    }
                },
                {
                    field: 'teams_indirect_status', title: '间接团队', align: 'center', width: 80, templet: function (d) {
                        if (!d.teams_indirect_status || d.teams_indirect_status < 1) return '-';
                        return laytpl('<b>{{d.teams_indirect_number}}</b>').render(d);
                    }
                },
                {
                    field: 'order_amount_status', title: '订单金额', align: 'center', width: 80, templet: function (d) {
                        if (!d.order_amount_status || d.order_amount_status < 1) return '-';
                        return laytpl('<b>{{d.order_amount_number}}</b>').render(d);
                    }
                },
                {
                    field: 'rebate_rule', title: '奖利规则', align: 'left', minWidth: 100, templet: function (d) {
                        return (d.html = ''), layui.each(d.rebate_rule || {}, function (k, rule) {
                            d.html += laytpl('<span class="layui-badge layui-bg-gray">{{d.v}}</span>').render({v: rule});
                        }), d.html || '-';
                    }
                },
                {field: 'status', title: '等级状态', align: 'center', width: 110, templet: '#StatusSwitchTpl'},
                {field: 'create_at', title: '创建时间', align: 'center', width: 170, sort: true},
                {toolbar: '#toolbar', title: '操作面板', align: 'center', minWidth: 160, fixed: 'right'},
            ]]
        });

        // 数据状态切换操作
        layui.form.on('switch(StatusSwitch)', function (obj) {
            var data = {id: obj.value, status: obj.elem.checked > 0 ? 1 : 0};
            $.form.load("{:url('state')}", data, 'post', function (ret) {
                if (ret.code < 1) $.msg.error(ret.info, 3, function () {
                    $('#UpgradeTable').trigger('reload');
                });
                return false;
            }, false);
        });
    });

</script>

<!-- 列表排序权重模板 -->
<script type="text/html" id="SortInputTpl">
    <input type="number" min="0" data-blur-number="0" data-action-blur="{:sysuri()}"
           data-value="id#{{d.id}};action#sort;sort#{value}" data-loading="false" value="{{d.sort}}"
           class="layui-input text-center">
</script>

<!-- 数据状态切换模板 -->
<script type="text/html" id="StatusSwitchTpl">
    <!--{if auth("state")}-->
    <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="已激活|已禁用" lay-filter="StatusSwitch"
           {{-d.status>0?'checked':''}}>
    <!--{else}-->
    {{-d.status ? '<b class="color-green">已启用</b>' : '<b class="color-red">已禁用</b>'}}
    <!--{/if}-->
</script>

<!-- 数据操作工具条模板 -->
<script type="text/html" id="toolbar">
    <!--{if auth('edit')}-->
    <a class="layui-btn layui-btn-primary layui-btn-sm" data-table-id="UpgradeTable" data-title="编辑等级"
       data-modal='{:url("edit")}?id={{d.id}}'>编 辑</a>
    <!--{/if}-->

    <!--{if auth("remove")}-->
    <a class="layui-btn layui-btn-danger layui-btn-sm" data-confirm="确定要删除问题吗?" data-action="{:url('remove')}"
       data-value="id#{{d.id}}">删 除</a>
    <!--{/if}-->
</script>
{/block}